//
//  CYEvaluateView.m
//  cyjf
//
//  Created by tauCross on 2017/7/10.
//  Copyright © 2017年 Auto Financing. All rights reserved.
//

#import "CYEvaluateView.h"



@interface CYEvaluateView ()

@property(nonatomic, strong)UIView *maskView;
@property(nonatomic, strong)UIView *contentView;

@end


@implementation CYEvaluateView

- (instancetype)init
{
    self = [super initWithFrame:[UIScreen mainScreen].bounds];
    if(self)
    {
        self.maskView = [[UIView alloc] initWithFrame:self.bounds];
        self.maskView.alpha = 0;
        self.maskView.backgroundColor = HEXCOLORA(@"000000", 0.5);
        [self addSubview:self.maskView];
        
        self.contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 295, 295)];
        self.contentView.bottom = 0;
        self.contentView.centerX = self.width / 2;
        self.contentView.backgroundColor = [UIColor whiteColor];
        self.contentView.layer.masksToBounds = YES;
        self.contentView.layer.cornerRadius = 4;
        [self addSubview:self.contentView];
        {
            UIImageView *topImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.contentView.width, 102)];
            topImageView.image = CYImage(@"evaluate_top_image");
            topImageView.left = 0;
            topImageView.top = 0;
            [self.contentView addSubview:topImageView];
            
            UILabel *titleLabel = [[UILabel alloc] init];
            titleLabel.textColor = HEXCOLOR(@"FFFFFF");
            titleLabel.font = [UIFont systemFontOfSize:18 weight:UIFontWeightMedium];
            titleLabel.text = @"风险评测";
            [titleLabel sizeToFit];
            titleLabel.left = 32;
            titleLabel.centerY = 50;
            [self.contentView addSubview:titleLabel];
            
            UILabel *descLabel = [[UILabel alloc] init];
            descLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightRegular];
            descLabel.textColor = HEXCOLOR(@"444444");
            descLabel.numberOfLines = 0;
            descLabel.text = @"为了更好评估您的风险承受能力，在投资前我们需要进行一次风险评测。";
            descLabel.size = [descLabel sizeThatFits:CGSizeMake(self.contentView.width - 40, MAXFLOAT)];
            descLabel.centerX = self.contentView.width / 2;
            descLabel.centerY = 46 + topImageView.bottom;
            [self.contentView addSubview:descLabel];
            
            UIButton *evaluateButton = [[UIButton alloc] init];
            [evaluateButton addTarget:self action:@selector(evaluate) forControlEvents:UIControlEventTouchUpInside];
            [evaluateButton setTitleColor:HEXCOLOR(@"FFFFFF") forState:UIControlStateNormal];
            evaluateButton.layer.masksToBounds = YES;
            evaluateButton.layer.cornerRadius = 5;
            [evaluateButton setBackgroundImage:[UIImage imageWithColor:APP_DEFAULT_COLOR_BLUE size:CGSizeMake(1, 1)] forState:UIControlStateNormal];
            [evaluateButton setTitle:@"开始评测" forState:UIControlStateNormal];
            evaluateButton.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightSemibold];
            evaluateButton.width = self.contentView.width - 40;
            evaluateButton.height = 40;
            evaluateButton.left = 20;
            evaluateButton.bottom = self.contentView.height - 48;
            [self.contentView addSubview:evaluateButton];
            
            UIButton *cancelButton = [[UIButton alloc] init];
            [cancelButton addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside];
            [cancelButton setTitle:@"稍后再评" forState:UIControlStateNormal];
            cancelButton.titleLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightRegular];
            [cancelButton setTitleColor:HEXCOLOR(@"888888") forState:UIControlStateNormal];
            cancelButton.frame = evaluateButton.frame;
            cancelButton.top = evaluateButton.bottom + 4;
            [self.contentView addSubview:cancelButton];
        }
    }
    return self;
}

- (void)show
{
    [[[[UIApplication sharedApplication] delegate] window] addSubview:self];
    APP_LOCK_USER_INTERACTION
    @weakify(self);
    [UIView animateWithDuration:0.25
                          delay:0
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         @strongify(self);
                         self.maskView.alpha = 1;
                         self.contentView.center = self.maskView.center;
                     }
                     completion:^(BOOL finished) {
                         APP_UNLOCK_USER_INTERACTION
                     }];
}

- (void)close
{
    APP_LOCK_USER_INTERACTION
    @weakify(self);
    [UIView animateWithDuration:0.25
                          delay:0
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         @strongify(self);
                         self.maskView.alpha = 0;
                         self.contentView.top = self.maskView.bottom;
                     }
                     completion:^(BOOL finished) {
                         @strongify(self);
                         [self removeFromSuperview];
                         APP_UNLOCK_USER_INTERACTION
                     }];
}

- (void)evaluate
{
    if(self.evaluateAction)
    {
        self.evaluateAction();
    }
    [self close];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    CGPoint point = [touches.anyObject locationInView:self];
    if(!CGRectContainsPoint(self.contentView.frame, point))
    {
        [self close];
    }
}

@end
